var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: '',
    teamList: [],//团队
    current: 1,//当前页
    size: 10,//一页显示条数
    moreInfo:true,//是否有数据
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getTeamList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  getTeamList(){
    var that = this;
    util.request(api.selectMyTeam, 
    { 
      userId: that.data.userId,
      current: that.data.current,
      size: that.data.size
    }).then(function(res){
      console.log("----"+JSON.stringify(res));
      var records = res.records; 
      var total = res.total;
      var moreInfo = true;
      if(total < that.data.size){
        moreInfo = false;
      }
      that.setData({
        teamList: res.records,
        moreInfo: moreInfo
      })
    });
  },
  //上拉获取更多列表
  hotActivityLoadMore: function (e) {
    var that = this
    that.listPage()
  },
  //分页查询列表
  listPage: function () {
    var that = this;
    //判断是否还有下一页
    if (that.data.moreInfo) {
      wx.showLoading({
        title: '加载中',
      })
      var current = that.data.current + 1;

      util.request(api.OrderList, {
        userId: that.data.userId,
        current: current,
        size: that.data.size
      }).then(function (res) {
        var l = res.records
        var total = JSON.parse(l.length)
        if (total < 9) {
          var moreInfo = false
        } else {
          var moreInfo = true
        }
        var lis = that.data.teamList

        console.log(res.records.length+"---------lis:" + lis.length );
        lis = lis.concat(res.records)
        console.log("---------lis:" + lis.length);
        that.setData({
          teamList: lis,
          current: current,
          moreInfo: moreInfo
        });
        wx.hideLoading();
      }).catch(function (res) {
        wx.hideLoading();
        console.log("获取数据失败！")
      })
    } else {
      wx.showToast({
        title: "已加载全部商品",
        icon: 'none',
        duration: 2000
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取用户的登录信息
    if (app.globalData.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');
      console.log('userInfo:' + JSON.stringify(userInfo));
      this.setData({
        userId: userInfo.id,
      });
    }

    this.getTeamList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})